package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uwlaser.domain.CraftPathDetail;
import com.uwlaser.service.mapper.CraftPathDetailMapper;
import com.uwlaser.utils.enums.DictValueEnum;
import lombok.AllArgsConstructor;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import com.uwlaser.domain.CraftPath;
import com.uwlaser.service.CraftPathService;
import com.uwlaser.service.dto.CraftPathDto;
import com.uwlaser.service.dto.CraftPathQueryParam;
import com.uwlaser.service.mapper.CraftPathMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author zk-Qin
* @date 2022-05-11
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = CraftPathService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class CraftPathServiceImpl extends CommonServiceImpl<CraftPathMapper, CraftPath> implements CraftPathService {

    // private final RedisUtils redisUtils;
    private final CraftPathMapper craftPathMapper;

    private final CraftPathDetailMapper craftPathDetailMapper;

    @Override
    public PageInfo<CraftPathDto> queryAll(CraftPathQueryParam query, Pageable pageable) {
        IPage<CraftPath> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<CraftPath> page = craftPathMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, CraftPathDto.class);
    }

    @Override
    public List<CraftPathDto> queryAll(CraftPathQueryParam query){
        return ConvertUtil.convertList(craftPathMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), CraftPathDto.class);
    }

    @Override
    public CraftPath getById(Integer id) {
        return craftPathMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public CraftPathDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), CraftPathDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(CraftPathDto resources) {
        CraftPath entity = ConvertUtil.convert(resources, CraftPath.class);

        //检查重复
        CraftPath craftPath = new CraftPath();
        craftPath.setPathCode(entity.getPathCode());
        craftPath.setVersion(entity.getVersion());
        Integer integer = craftPathMapper.selectCount(new QueryWrapper<>(craftPath));
        if(integer > 0){
            throw new RuntimeException("工艺路线编号【"+entity.getPathCode()+"】版本【"+entity.getVersion()+"】已存在!");
        }

        //检查启用状态
        if(DictValueEnum.START.getValue().equals(entity.getIsDisable())){
            //如果是启用，禁用其他
            CraftPath update = new CraftPath();
            update.setIsDisable(DictValueEnum.DISABLED.getValue());

            CraftPath warpper = new CraftPath();
            warpper.setProductCode(entity.getProductCode());
            craftPathMapper.update(update,new QueryWrapper<>(warpper));
        }
        return craftPathMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(CraftPathDto resources){
        CraftPath entity = ConvertUtil.convert(resources, CraftPath.class);

        //检查启用状态
        if(DictValueEnum.START.getValue().equals(entity.getIsDisable())){
            //如果是启用，禁用其他
            CraftPath update = new CraftPath();
            update.setIsDisable(DictValueEnum.DISABLED.getValue());

            CraftPath warpper = new CraftPath();
            warpper.setProductCode(entity.getProductCode());
            craftPathMapper.update(update,new QueryWrapper<>(warpper));
        }

        int ret = craftPathMapper.updateById(entity);

        //检查重复
        CraftPath craftPath = new CraftPath();
        craftPath.setPathCode(entity.getPathCode());
        craftPath.setVersion(entity.getVersion());
        Integer integer = craftPathMapper.selectCount(new QueryWrapper<>(craftPath));
        if(integer >= 2){
            throw new RuntimeException("工艺路线编号【"+entity.getPathCode()+"】版本【"+entity.getVersion()+"】已存在!");
        }
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids){
        // delCaches(ids);
        for (Integer id : ids) {
            CraftPathDetail craftPathDetail = new CraftPathDetail();
            craftPathDetail.setPathId(id);
            craftPathDetailMapper.delete(new QueryWrapper<>(craftPathDetail));
        }
        return craftPathMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<CraftPathDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (CraftPathDto craftPath : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("路线编码", craftPath.getPathCode());
              map.put("路线名称", craftPath.getPathName());
              map.put("产品编码", craftPath.getProductCode());
              map.put("版本", craftPath.getVersion());
              map.put("是否禁用", craftPath.getIsDisable());
              map.put("描述", craftPath.getRemark());
              map.put("创建人", craftPath.getCreateBy());
              map.put("创建时间", craftPath.getCreateTime());
              map.put("更新人", craftPath.getUpdateBy());
              map.put("更新时间", craftPath.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
